package org.helian.task;

import org.helian.controller.UserController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Description 登录定时器
 * @Author 墨染
 * @Date 2020/11/7 19:52
 * @Version 1.0
 */
@Component //加入spring管理
public class LoginTask {

    private static final Logger logger = LoggerFactory.getLogger(LoginTask.class);

    @Autowired
    private RedisTemplate redisTemplate;

    /***
     * 登录定时器
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void loginDue() {
        // 取这个登录的key
        String login_key = (String) redisTemplate.opsForValue().get("login_key");
        if (login_key != null) { // 如果存在说明登录状态 清空session
            logger.info("当前登录已过期..........");
            UserController.sessionMap.clear();
            redisTemplate.delete("login_key");
        } else { // 未登录就是未登录不需要清空session
            logger.info("未登录..........");
        }
    }
}
